package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.web.page.TableSupport;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/plus/dict/data")
public class DictDataController extends BaseController {
    @Autowired
    private DictDataService dictDataService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData) {
        Page<SysDictData> page = new Page<>();
        page.setCurrent(Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_NUM), 1));
        page.setSize(Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_SIZE), 10));
        IPage<SysDictData> iPage  = dictDataService.selectDictDataList(page,dictData);
        return getDataTable(iPage.getRecords(),iPage.getTotal());
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict) {
        System.out.println("添加字典数据咯");
        dict.setCreateBy(SecurityUtils.getUsername());
        dict.setCreateTime(new Date());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 获取字典数据列表
     */
    @GetMapping("/select/by/type/{dictType}")
    public TableDataInfo selectByDictType(@PathVariable String dictType){
        return getDataTable(dictDataService.selectByDictType(dictType));
    }

}
